package com.shiyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.shiyi.core.vo.Menu;
import com.shiyi.system.entity.SysMenu;
import com.shiyi.system.mapper.SysMenuMapper;
import com.shiyi.system.service.SysMenuRoleService;
import com.shiyi.system.service.SysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author shiyi
 * @since 2020-11-19
 */
@Service
@Transactional
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysMenuRoleService sysMenuRoleService;

    @Override
    public List<Menu> listMenu() {
        return this.baseMapper.listMenu();
    }

    @Override
    public List<Menu> listMenusByRoleIds(List<Integer> roleIds) {
        List<Integer> menuIds = sysMenuRoleService.queryMenuIdsByRoleIds(roleIds);
        return this.baseMapper.listMenusByMenuIds(menuIds);
    }

    @Override
    public boolean getMenuByMenuName(String menuName) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("menu_name", menuName);
        return this.getOne(queryWrapper) == null;
    }

    @Override
    public boolean insertMenu(SysMenu menu) {
        menu.setCreateTime(new Date());
        return this.save(menu);
    }

    @Override
    public boolean getMenuByMenuNameAndId(String menuName, Integer id) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("menu_name", menuName);
        queryWrapper.ne("id", id);
        return this.getOne(queryWrapper) == null;
    }

    @Override
    public boolean deleteMenu(Integer id) {
        UpdateWrapper<SysMenu> wrapper = new UpdateWrapper<>();
        wrapper.set("is_delete", 2);
        wrapper.eq("id", id);
        return this.update(wrapper);
    }
}
